package com.ptl.bp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ptl.bp.config.ApiResult;
import com.ptl.bp.entity.ErrorLogEntity;
import com.ptl.bp.service.ErrorReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/errorReport")
@RequiredArgsConstructor
@Tag(name = "错误上报API")
public class ErrorReportController {

    private final ErrorReportService errorReportService;

    @PostMapping
    @Operation(summary = "上报客户端错误")
    public ApiResult<Long> reportError(@RequestBody ErrorLogEntity entity) {
        return ApiResult.success(errorReportService.reportError(entity));
    }

    @Operation(summary = "分页查询论坛", description = "分页查询行业论坛列表")
    @GetMapping("/page")
    public ApiResult<IPage<ErrorLogEntity>> page(@Parameter(description = "分页参数") Page<ErrorLogEntity> page,
                                                 @Parameter(description = "查询条件") ErrorLogEntity entity) {
        page = errorReportService.lambdaQuery()
                .like(entity.getMessage() != null, ErrorLogEntity::getMessage, entity.getMessage())
                .orderByDesc(ErrorLogEntity::getCreateTime)
                .page(page);
        return ApiResult.success(page);
    }
}